﻿using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Runtime.Serialization;
using System.IO;
using WeatherService.Helpers;
using WeatherService.Types;

namespace WeatherService.Storage
{
    public static class IsolatedStorageCacheManager<T>
    {


        public static void Store(string filename, T obj)
        {
            try
            {
                using (var appStore = IsolatedStorageFile.GetMachineStoreForAssembly())
                {
                    using (var fileStream = appStore.OpenFile(filename, FileMode.Create))
                    {
                        var serializer = new DataContractSerializer(typeof(T));
                        serializer.WriteObject(fileStream, obj);
                        fileStream.Close();
                        fileStream.Dispose();
                    }
                    appStore.Close();
                    appStore.Dispose();
                }
            }
            catch (FileNotFoundException fileNotFound)
            {
                try
                {


                    using (var appStore = IsolatedStorageFile.GetMachineStoreForAssembly())
                    {
                        using (var fileStream = appStore.CreateFile(filename))
                        {
                            var serializer = new DataContractSerializer(typeof(T));
                            serializer.WriteObject(fileStream, obj);
                            fileStream.Close();
                            fileStream.Dispose();
                        }
                        appStore.Close();
                        appStore.Dispose();
                    }
                }
                catch
                {
                    //sink error
                }
            }
            catch (Exception ex)
            {
               Logger.ErrorLog("Error occurred saving data to isolated storage: " + ex.Message);
            }

        }

        public static void ClearAll()
        {
            using (var appStore = IsolatedStorageFile.GetMachineStoreForAssembly())
            {
                appStore.Remove();
            }
        }
        public static void ClearFile(string startsWith)
        {
            using (var appStore = IsolatedStorageFile.GetMachineStoreForAssembly())
            {
                foreach (var c in appStore.GetFileNames(startsWith + "*"))
                {
                    appStore.DeleteFile(c);
                }
            }

        }
        public static string[] GetFiles(string startsWith)
        {
            string[] list;
            using (var appStore = IsolatedStorageFile.GetMachineStoreForAssembly())
            {
                list = appStore.GetFileNames(startsWith + "*");
            }
            return list;

        }
        public static T Retrieve(string filename)
        {



            T obj = default(T);
            try
            {


                using (var appStore = IsolatedStorageFile.GetMachineStoreForAssembly())
                {
                    if (appStore.FileExists(filename))
                    {
                        using (var fileStream = appStore.OpenFile(filename, FileMode.Open))
                        {
                            var serializer = new DataContractSerializer(typeof(T));
                            obj = (T)serializer.ReadObject(fileStream);
                            fileStream.Close();
                            fileStream.Dispose();
                        }
                    }
                    appStore.Close();
                    appStore.Dispose();
                }
            }
            catch (Exception ex)
            {
               Logger.ErrorLog("Error occurred retrieving data from isolated storage: " + ex.Message);
            }
            return obj;
        }

        #region DataListXMLSerialization
        static public void SerializeDataListToXML(List<Weather> data, string name)
        {
            IsolatedStorageCacheManager<List<Weather>>.Store(name + "DataList.xml", data);
        }

        static public List<Weather> DeserializeDataFromXML(string name)
        {
            var status = IsolatedStorageCacheManager<List<Weather>>.Retrieve(name + "DataList.xml");
            return status;
        }
        
        #endregion
    }
}
